<template>
    <div class="login">
        <Navbar title="登录">
            <template>
                <span @touchstart='$router.replace("/register")'>注册</span>
            </template>
        </Navbar>
        <van-form @submit="onLogin">
            <van-field
                v-model.trim="username"
                name="用户名"
                label="用户名"
                placeholder="用户名"
                :rules="[{ required: true, message: '请填写用户名' }]"
            />
            <van-field
                v-model.trim="password"
                type="password"
                name="密码"
                label="密码"
                placeholder="密码"
                :rules="[{ required: true, message: '请填写密码' }]"
            />
            <div style="margin: 16px;">
                <van-button round block type="info" native-type="submit">登录</van-button>
            </div>
        </van-form>
    </div>
</template>
<script>
import { 
    Form,
    Field,
    SubmitBar,
    Button,
} from 'vant';
import {Navbar} from '@/components/'
export default {
   data() {
      return {
          username:'',
          password:''
      }
   },
   components:{
       [Form.name]:Form,
       [Field.name]:Field,
       [SubmitBar.name]:SubmitBar,
       [Button.name]:Button,
       Navbar,
   },
   computed:{
   },
   methods:{
       onLogin(){
           var data={
               username:this.username,
               password:this.password
           }
           this.$http.fetchLogin(data).then(res=>{
               console.log(res);
               //登陆成功后，把token绑定在本地存储上面
               if(res){
                   localStorage.setItem('token',res.token)
                   this.$toast.success('登陆成功')
                   this.timer=setTimeout(()=>{
                       this.$router.back()
                   },2000)
               }
           })
       }
   },
   beforeDestroy(){
       clearTimeout(this.timer)
   }
}
</script>
<style lang="scss" scoped>

</style>
